package com.example.wendao.view.ui.fragment;


import android.graphics.Color;
import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import androidx.viewpager2.widget.ViewPager2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.wendao.R;
import com.example.wendao.databinding.FragmentNewsBinding;
import com.example.wendao.model.entity.News;
import com.example.wendao.view.adapter.NewsAdapter;
import com.example.wendao.view.adapter.TestLoopAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class NewsFagment extends Fragment {
    private RollPagerView mRollViewPager;
    private List<News> newsList = new ArrayList<>();
    private FragmentNewsBinding fragmentNewsBinding;
    private ViewPager2 vp;

    private TabLayout tb;

    private NewsAdapter newsAdapter;

    public NewsFagment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //初始化databiding
        fragmentNewsBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_news, container, false);
        View view = fragmentNewsBinding.getRoot();
        vp = fragmentNewsBinding.newsViewPager;
        tb = fragmentNewsBinding.newTabs;

        //搜索点击点击事件
        fragmentNewsBinding.newsSearchImageView.setOnClickListener(view1 -> Navigation.findNavController(view1).navigate(R.id.action_fragment_new_to_fragment_search));

        //初始化轮播图View
        mRollViewPager = fragmentNewsBinding.rollPagerView;
        //定义轮播图指示器
        mRollViewPager.setHintView(new ColorPointHintView(view.getContext(), Color.YELLOW, Color.WHITE));
        // 隐藏指示器

        //加轮播监听
        mRollViewPager.setOnItemClickListener(position -> Toast.makeText(view.getContext(), "Item " + position + " clicked", Toast.LENGTH_SHORT).show());
        mRollViewPager.setAdapter(new TestLoopAdapter(mRollViewPager));
        //开启轮播
        mRollViewPager.isPlaying();
        newsAdapter = new NewsAdapter(this);
        // 设置ViewPager2适配器
        vp.setAdapter(newsAdapter);

        // 绑定 TabLayout 与 ViewPager2，第三个参数设置tab的图标、文本等参数
        new TabLayoutMediator(tb, vp,
                (tab, position) -> {
                    tab.setText(getTabTitle(position));
                }).attach();

        return view;
    }

    //假数据
    private void initNews() {
        News news_a = new News();
        news_a.setNewsAgree(10);
        news_a.setNewsAgreeId("10");
        news_a.setNewsAuthorId(100);
        news_a.setNewsCoin(3);
        news_a.setNewsId(1);
        news_a.setNewsTitle("八十老汉生龙活虎的秘密");
        news_a.setNewsCollect(30);
        news_a.setNewsTime("2020-6-26");
        news_a.setNewsContent("震惊，原因竟然是这样的！快转给你的亲朋好友");
        newsList.add(news_a);
        News news_b = new News();
        news_b.setNewsAgree(3);
        news_b.setNewsAgreeId("5");
        news_b.setNewsAuthorId(170);
        news_b.setNewsCoin(3);
        news_b.setNewsId(8);
        news_b.setNewsTitle("八十老太为何半夜哭泣");
        news_b.setNewsCollect(1000);
        news_b.setNewsTime("2020-6-26");
        news_b.setNewsContent("震惊，原因竟然是这样的！快转给你的亲朋好友");
        newsList.add(news_b);
    }

    private List<String> titles = new ArrayList<>();

    {
        Collections.addAll(titles, "要闻", "科技", "信息", "美食", "推荐", "山东", "汽车", "学习");
    }

    private String getTabTitle(int position) {
        return titles.get(position);
    }

}
